package dev.ece.suit.hexmap;

import java.util.ArrayList;
import java.util.List;

import dev.ece.core.texture.Texture;
import dev.ece.suit.hexmap.terrain.TerrainRenderer;
import dev.ece.suit.hexmap.terrain.type.TerrainType;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.tools.Direction;

/**
 * 地图片元
 * @author 潜行的虚影
 *
 */
@Deprecated
public class HexMapCell {
	
	/**地图坐标**/
	private int row;
	
	private int col;
	
	/**
	 * 相邻地形，没有为null，逆时针顺序：0 左上、1 左、2 左下、3 右下、4 右、5 右上  
	 */
	private HexMapCell[] cells = new HexMapCell[6];
	
	/**
	 * 相邻接缝，没有为null，逆时针顺序：0 左上、1 左、2 左下、3 右下、4 右、5 右上  
	 */
	private HexMapCrevice[] crevices = new HexMapCrevice[6];
	
	/**
	 * 相邻三角接缝，没有为null，逆时针顶点顺序：0 上、1 左上、2 左下、3 下、4 右下、5 右上
	 */
	private HexMapTriangle[] triangles = new HexMapTriangle[6];
	
	/**
	 * 地形
	 */
	private TerrainType terrainType;
	
	private int first;
	
	/**
	 * 顶点
	 */
	private final List<Vector3f> vertice = new ArrayList<Vector3f>();
	
	/**
	 * 纹理坐标
	 */
	private final List<Vector2f> uv = new ArrayList<Vector2f>();
	
	/**
	 * 法线
	 */
	private final List<Vector3f> normals = new ArrayList<Vector3f>();
	/**
	 * 渲染器
	 */
	private TerrainRenderer renderer;
	
	private HexMapCell() {}
	
	public void render(HexMapCellShader hexMapCellShader, Texture[] textures) {
		hexMapCellShader.glMaterial(getTerrainType().getMaterial());
		hexMapCellShader.glTexture(textures[getTerrainType().getTextureIndex()]);
		getTerrainType().glShader(hexMapCellShader);
		renderDepthMap();
	}
	
	public void renderDepthMap() {
		renderer.render(this.getFirst());
	}
	
	public boolean isIntersected(Vector3f o, Vector3f dir, HexMapPickUp hexMapPickUp) {
		boolean isIntersected = renderer.isIntersected(o, dir, vertice, hexMapPickUp.getPoint());
		if(isIntersected) {
			hexMapPickUp.setCell(this);
			if(this.terrainType.hasWater()) {
				//水面高度0
				hexMapPickUp.getPoint().setY(Math.max(hexMapPickUp.getPoint().getY(), 0));
			}
		}
		return isIntersected;
	}
	
	/**
	 * 
	 * @param renderer 混合透明度
	 * @param terrainType 地形
	 * @param row
	 * @param col
	 * @return
	 */
	public static HexMapCell createHexMapCell(TerrainRenderer renderer, TerrainType terrainType, int row, int col) {
		HexMapCell cell = new HexMapCell();
		cell.renderer = renderer;
		cell.terrainType = terrainType;
		cell.row = row;
		cell.col = col;
		return cell;
	}
	
	/**
	 * 获取方向上的 元素角标
	 * @param rows
	 * @param cols
	 * @param row
	 * @param col
	 * @param director 方向:Direction.DIRECTION_0 右上，1 右侧，2 右下，3 左下， 4左侧，5左上
	 * @return
	 */
	public static HexMapCellIndex getHexMapCell(int rows, int cols, int row, int col, int director) {
		int t_row = row;
		int t_col = col;
		int direction = Direction.getDirection(director);
		//偶数行
		boolean even = row % 2 == 0;
		if(direction == Direction.DIRECTION_0) {
			//右上
			t_row = row - 1;
			if(even) {
				t_col = col;
			} else {
				t_col = col + 1;
			}
		} else if(direction == Direction.DIRECTION_1) {
			//右侧
			t_col = col + 1;
		} else if(direction == Direction.DIRECTION_2) {
			//右下
			t_row = row + 1;
			if(even) {
				t_col = col;
			} else {
				t_col = col + 1;
			}
		} else if(direction == Direction.DIRECTION_3) {
			//左下
			t_row = row + 1;
			if(even) {
				t_col = col - 1;
			} else {
				t_col = col;
			}
		} else if(direction == Direction.DIRECTION_4) {
			//左侧
			t_col = col - 1;
		} else if(direction == Direction.DIRECTION_5) {
			t_row = row - 1;
			if(even) {
				t_col = col - 1;
			} else {
				t_col = col;
			}
		}
		if(t_row > -1 && t_row < rows && t_col > -1 && t_col < cols) {
			return new HexMapCellIndex(t_row, t_col);
		}
		return null;
	}
	
	public void setup() {
		renderer.setup();
		normals.clear();
		uv.clear();
	}

	public TerrainRenderer getRenderer() {
		return renderer;
	}

	public List<Vector3f> getVertice() {
		return vertice;
	}

	public List<Vector2f> getUv() {
		return uv;
	}

	public List<Vector3f> getNormals() {
		return normals;
	}

	public int getRow() {
		return row;
	}

	public int getCol() {
		return col;
	}

	public HexMapCell[] getCells() {
		return cells;
	}

	public HexMapCrevice[] getCrevices() {
		return crevices;
	}

	public HexMapTriangle[] getTriangles() {
		return triangles;
	}

	public TerrainType getTerrainType() {
		return terrainType;
	}

	public Vector3f getCenter() {
		Vector3f center = this.getVertice().get(0);
		return new Vector3f(center.getX(), this.getCenterHeight(), center.getZ());
	}
	
	public float getCenterHeight() {
		if(this.getTerrainType().hasWater()) {
			return Math.max(0, this.getVertice().get(0).getY());
		}
		return this.getVertice().get(0).getY();
	}

	protected int getFirst() {
		return first;
	}

	protected void setFirst(int first) {
		this.first = first;
	}

	public void destroy() {
		vertice.clear();
	}

	@Override
	public boolean equals(Object obj) {
		return this == obj;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for(Vector3f vec: vertice) {
			builder.append(vec.toString()).append("\n");
		}
		return builder.toString();
	}
	
}
